var util = require("../../../utils/util.js"),
  {
    api
  } = require("../../../utils/api.js"),
  app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    staticPath: app.globalData.staticPath + '/static/wx/images',
    sexList: [{
      name: '男'
    }, {
      name: '女'
    }],
    sexShow: false,
    userInfo: {}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    wx.hideShareMenu();
    this.getUsers();
  },
  uploadHeaderImg() {
    wx.navigateTo({
      url: '/pages/mine/userInfo/headerImage',
    })
  },
  async getUsers() {
    const data = await app.getUsers();
    data && this.setData(data);
  },
  onClose() {
    this.setData({
      sexShow: false
    })
  },
  onSexSelect(e) {
    this.data.userFrom.sex = e.detail.name;
    this.setData({
      userFrom: this.data.userFrom
    })
  },
  setnickname(e) {
    this.data.userInfo.nickname = e.detail.value;
    this.setData({
      userInfo: this.data.userInfo
    })
  },
  changeUserImg(url) {
    this.data.userInfo.avatar = url.fullurl;
    this.data.userInfo.uploadAvatar = url.url;
    this.setData({
      userInfo: this.data.userInfo
    });
  },
  async submit() {
    const {
      nickname,
      uploadAvatar
    } = this.data.userInfo;
    if (!uploadAvatar) return wx.showToast({
      title: '请上传头像',
      icon: 'none'
    })
    // if (!nickname) return wx.showToast({
    //   title: '请填写头像',
    //   icon: 'none'
    // })
    let {
      code
    } = await api.profileUsers({
      avatar: uploadAvatar
    });
    if (code == 1) {
      wx.showToast({
        title: '操作成功',
        icon: 'success'
      })
      setTimeout(() => wx.navigateBack(), 1500)
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage(e) {
    return util.shareEvent(e)
  }
})